package com.foreknow.demo13;

/**
 * 订单类 Order
 */
public class Order {
    private String orderId; //订单编号
    private double total; //订单总额
    private OrderItem[] items; //保存多个订单项

    public Order(String orderId) {
        this.orderId = orderId;
        this.items = new OrderItem[3];
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public double getTotal() {
        //调用计算总额的方法
        calTotal();
        return total; //返回计算后的总额
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public OrderItem[] getItems() {
        return items;
    }

    //通过下标将OrderItem对象保存到OrderItem[]数组中 i表示数组的下标
    public void setItems(OrderItem items,int i) {
        this.items[i] = items;
    }

    /**
     * 计算订单总额 = 单价 * 数量
     */
    public void calTotal() {
        double total = 0;
        //对数组遍历
        if(items!=null) {
            for(int i = 0;i<items.length;i++) {
                total+= items[i].getNum() * items[i].getPprice();
            }
        }
        this.total = total;
    }

}
